package com.shuorikeji.huohuo.ui.me;

import android.os.Bundle;
import android.widget.ImageView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MsgAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MsgInfoBean;
import com.shuorikeji.huohuo.bean.MsgMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MsgActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private int curPage;
    private List<MsgInfoBean> msgList = new ArrayList<>();
    private MsgAdapter msgAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_msg);
    }

    @Override
    protected void initData() {
        initSmartRefreshLayout();
        initAdapter();
        curPage = 1;
        getMyMsg();
        ivBack.setOnClickListener(view -> {
            finish();
        });
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(MsgActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getMyMsg();
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getMyMsg();
            }
        });
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 15, 0));
        msgAdapter = new MsgAdapter(mContext, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {

            }
        });
        rvData.setAdapter(msgAdapter);
    }

    private void getMyMsg() {
        mPresenter.myMsg(true, 20, curPage, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlView.finishLoadMore();
                srlView.finishRefresh();
                switch (flag) {
                    case 1:
                        MsgMainBean data = (MsgMainBean) object;
                        if (curPage == 1) {
                            msgList.clear();
                        }
                        msgList.addAll(data.getData().getData());
                        msgAdapter.setData(msgList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MsgActivity.this, msg);
                        break;
                }
            }
        });
    }
}